<?php
/*
	异步支付通知处理
*/
define('NOSESSION',true);
require '../../core/core.php';
set_exi(function($message='success',$type='success'){
    //如果存在错误，此处整理错误日志，后期完善
    if($type=='error'){
        $log=date('Y/m/d H:i:s');
        $log.="\t{$_SESSION['uniacid']}\t{$_SESSION['uid']}\t{$message}\n";
        file_put_contents('error_log.txt',$log,FILE_APPEND);
    }
    echo 'error';
    exit;
});
if($_POST){
    $log2=ijson_encode($_POST);
    file_put_contents('error_log.txt',$log2."\n",FILE_APPEND);
    $bodydata=json_decode($_POST['data'],true);
    $body=$bodydata['body'];
}else{
    $body= $_RQ;
    $ishema=true;
}
//file_put_contents('error_log.txt',ijson_encode($body)."\n",FILE_APPEND);
$setting=plugin('jy_aliesign')->plugin_setting('basic');
    if($_SERVER['REQUEST_METHOD'] != 'POST'){
        exi('非法回调','error');
    }
//    校验签名 如果header里放入的值为X_TSIGN_OPEN_SIGNATURE，到header里会自动加上HTTP_，并且转化为大写，取值时如下
    if(!isset($_SERVER['HTTP_X_TSIGN_OPEN_SIGNATURE'])){
		exi('签名不能为空','error');
    }
    $sign =  $_SERVER['HTTP_X_TSIGN_OPEN_SIGNATURE'];
    $secret = $setting['secret'];//应用对应密钥
    //1.获取时间戳的字节流
    if(!isset($_SERVER['HTTP_X_TSIGN_OPEN_TIMESTAMP'])){
		exi('时间戳不能为空','error');
    }
    $timeStamp =  $_SERVER['HTTP_X_TSIGN_OPEN_TIMESTAMP'];
 
    //2.获取query请求的字节流，对 Query 参数按照字典对 Key 进行排序后,按照value1+value2方法拼接
    $params = $_GET;
    if(!empty($params)){
        ksort($params);
    }
 
    $requestQuery = '';
    foreach($params as $val){
        $requestQuery .= $val;
    }
    //4.组装数据并计算签名
	$body=ijson_encode($body);
    $data = $timeStamp . $requestQuery . $body;
    $mySign = hash_hmac("sha256", $data, $secret);
    if($mySign != $sign){
		//exi('验签失败','error');
    }else{
		//exi('验签成功','error');
    }
    $result = json_decode($body,true);
    switch ($result['action']){
        case 'SIGN_MISSON_COMPLETE':
            //签署人签署完成回调
			model('jy_aliesign/model')->uplog($flow_id,1);
            break;
        case 'SIGN_FLOW_COMPLETE':
            //流程结束逻辑处理
            break;
    }